package com.koron.css2.newProjectCenter.impl.query;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.query.PjQueryQualifiedBean;
import com.koron.css2.newProjectCenter.mapper.PjQueryQualifiedMapper;
import com.koron.css2.newProjectCenter.vo.query.PjQueryQualifiedVo;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 合格率查询入口
 */
public class PjQueryQualified implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        PjQueryQualifiedBean bean = JsonUtils.objectToPojo(req.getData(), PjQueryQualifiedBean.class);
        PjQueryQualifiedMapper mapper = factory.getMapper(PjQueryQualifiedMapper.class);
        if (bean.getTodo() != null && bean.getTodo() == 2) {
            List<PjQueryQualifiedBean> updateList = bean.getList();
            if (updateList != null && !updateList.isEmpty()) {
                for (PjQueryQualifiedBean item : updateList) {
                    if (item.getRegisterNo() == null)
                        return MessageBean.create(Constant.ILLEGAL_PARAMETER, "参数错误", null);
                    item.setUpdateName(userInfo.getUserInfo().getName());
                    item.setUpdateAccount(userInfo.getUserInfo().getAcount());
                }
                updateList.forEach(item -> {
                    Boolean isExist = mapper.isExistById(item);
                    if (isExist) {
                        mapper.updateQualified(item);
                    } else {
                        mapper.insertQualified(item);
                    }
                });
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", null);
        }
        Map<String, String> PPT = Tools.mapDicByCode(factory, "PPT");
        PageHelper.startPage(bean.getPage(), bean.getPageCount());
        List<PjQueryQualifiedVo> list = mapper.getList(bean);
        list.forEach(item -> {
            item.setBusinessType(PPT.get(item.getBusinessType()));
            if (item.getUserFeature() != null && item.getUserFeature().equals("1")) {
                item.setUserFeature("普通客户");
            } else if (item.getUserFeature() != null && item.getUserFeature().equals("2")) {
                item.setUserFeature("大客户");
            }
        });
        PageInfo<PjQueryQualifiedVo> pageInfo = new PageInfo<>(list);
        MessageBean result = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
        result.setData(pageInfo);
        return result;
    }
}
